package com.huiquan.framework.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

public class RespHeaderUtil {

	private static final String CONTENT_TYPE_XLS = "application/vnd.ms-excel";
	private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
	private static final String CONTENT_TYPE_TXT = "text/plain";
	private static final String HEADER_DIS = "Content-Disposition";
	private static final String HEADER_ATT = "attachment;filename=";
	private static final String FILE_SUFFIX_XLS = ".xls";
	private static final String FILE_SUFFIX_XLSX = ".xlsx";
	private static final String FILE_SUFFIX_TXT = ".txt";
	
	public static String setXlsxResp(HttpServletResponse resp, String fileName) {
		resp.reset();
		resp.setContentType(CONTENT_TYPE_XLSX);
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String fileFullName = fileName + FILE_SUFFIX_XLSX;
		resp.setHeader(HEADER_DIS, HEADER_ATT + fileFullName);
		
		return fileFullName;
	}
	
	public static String setXlsResp(HttpServletResponse resp, String fileName) {
		resp.reset();
		resp.setContentType(CONTENT_TYPE_XLS);

		String fileFullName = fileName + FILE_SUFFIX_XLS;
		resp.setHeader(HEADER_DIS, HEADER_ATT + fileFullName);
		
		return fileFullName;
	}

	public static String setTxtResp(HttpServletResponse resp, String fileName) {
		resp.reset();
		resp.setContentType(CONTENT_TYPE_TXT);
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String fileFullName = fileName + FILE_SUFFIX_TXT;
		resp.setHeader(HEADER_DIS, HEADER_ATT + fileFullName);
		
		return fileFullName;
	}
	
}
